<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改项目定义' : '新增项目定义'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="100px" :rules="rules">
      <el-form-item label="上级项目">
        <el-select clearable style="width: 100%" v-model="form.parentItemId" placeholder="请选择">
          <el-option
            v-for="item in selectData"
            :key="item.moralityItemId"
            :value="item.moralityItemId"
            :label="item.moralityItemName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="moralityItemName">
        <el-input v-model="form.moralityItemName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目类型" prop="moralityItemType">
        <el-select clearable style="width: 100%" v-model="form.moralityItemType" placeholder="请选择">
          <el-option :value="1" label="基础项目" />
          <el-option :value="2" label="一票认定差项目" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目标准分" prop="itemStandard">
        <el-input
          type="number"
          v-model.number="form.itemStandard"
          :disabled="!(form.moralityItemType !== 2 && form.leaf !== 0)"
        />
      </el-form-item>
      <el-form-item label="排序" prop="itemOrder">
        <el-input type="number" v-model.number="form.itemOrder" />
      </el-form-item>
      <el-form-item label="是否末级" prop="leaf">
        <el-radio-group v-model="form.leaf">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { moralityCreate, moralityUpdate, moralityAll } from '@/api/morality'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      visible: false,
      isEdit: false,
      form: {
        parentItemId: null,
        moralityItemName: null,
        moralityItemType: null,
        itemStandard: null,
        itemOrder: 0,
        leaf: null
      },
      selectData: [],
      rules: {
        moralityItemName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        moralityItemType: [{ required: true, message: '请选择项目类型', trigger: 'blur' }],
        leaf: [{ required: true, message: '请选择是否末级', trigger: 'blur' }],
        itemOrder: [{ type: 'number', message: '请输入数字', trigger: 'blur' }],
        itemStandard: [{ type: 'number', message: '请输入数字', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    moralityAll().then(res => {
      this.selectData = res.data
    })
  },
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
      if (!bool) {
        this.$set(this.form, 'itemOrder', 0)
        this.$set(this.form, 'leaf', 0)
      }
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await moralityUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await moralityCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
